const userService = require('../../services/userService');
const Api= require('../../services/api');
import Toast from '../../vant/toast/toast';
import Dialog from '../../vant/dialog/dialog';
var app = getApp()
Page({
  data: {
      assets:app.assets,
      assetsByEnv:app.assetsByEnv,
      orderVO:{},
      otherimgs:[],
      previewImgs:[],
      bcNum: 0,
      id:''
  },
    previewImg: function (e) {
        var src = e.currentTarget.dataset.src;
        wx.previewImage({
            current: src, // 当前显示图片的http链接
            urls: this.data.previewImgs // 需要预览的图片http链接列表
        })
    },
  onShow: function(){

  },
    goHome:function(){
        wx.reLaunch({
            url:'../home/home'
        })
    },
  onLoad: function (options) {
      wx.setNavigationBarTitle({
          title:'鞋库鉴定'
      })
    var id = options.id;

    if (id) {
        this.setData({
            id:id
        });
        this._getDetail(id);
    }
  },
    _getDetail:function(id){
        var params = {
            id:id
        };
        userService.getAppra(params).then((data=>{
            if(data.result) {
                let order = data.obj.order;
                let previews = [];
                this.setData({
                    orderVO:data.obj
                });

                if (order.img1) {
                    previews.push(order.img1);
                }
                if (order.img2) {
                    previews.push(order.img2);
                }
                if (order.img3) {
                    previews.push(order.img3);
                }
                if (order.img4) {
                    previews.push(order.img4);
                }
                if (order.img5) {
                    previews.push(order.img5);
                }
                if (order.img6) {
                    previews.push(order.img6);
                }
                if (order.img7) {
                    previews.push(order.img7);
                }

                if (order.img8) {
                    previews.push(order.img8);
                }

                if (data.obj.order.otherimg) {
                    previews = previews.concat(data.obj.order.otherimg.split(','));
                    this.setData({
                        otherimgs:data.obj.order.otherimg.split(',')
                    });
                }
                this.setData({
                    previewImgs:previews
                });

            }else{
                wx.showToast({title:data.errorMsg,icon:'none'});
            }
        }),(error=>{
            wx.showToast({title:'getAppra接口异常',icon:'none'});
    }));
    },
    onShareAppMessage: function () {
        return {
            imageUrl:this.data.assetsByEnv+'/tanchuang1.png',
            title: '鞋库鉴定',
            path: '/pages/appraDetail/appraDetail?id='+this.data.id,
            success: function(res) {
            },
            fail: function(res) {
            }
        }
    },
    upload:function(e) {
        var field = e.currentTarget.dataset.field;
        var that = this;
        var user = wx.getStorageSync('user');
        wx.chooseImage({
            count: 1,
            fail:function(res){
                //非用户取消上传错误
                if(res.errMsg != 'chooseImage:fail cancel') {
                    wx.showToast({title:'选择图片失败'+res.errMsg,icon:'none'});
                }
            },
            success: function(res) {
                Toast.loading({
                    mask: true,
                    message: '图片上传中...',
                    duration:0
                });
                var tempFilePaths = res.tempFilePaths;
                var formData = {};
                // formData.openid=user.openid;
                // console.error(formData);
                var uploadTask = wx.uploadFile({
                    url: Api.upload, //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    header: {
                        'content-type':'multipart/form-data'
                    },
                    formData:formData,
                    name: 'file',
                    success: function(res){
                        Toast.clear();
                        if (res.statusCode==200) {
                            var data = res.data;
                            data = JSON.parse(data);
                            if (data.result) {
                                var name = data.obj.name;
                                // console.error(showUrl);
                                if (field == 'imgothers') {
                                    that.data.otherimgs.push(Api.download + name);
                                    let num = that.data.bcNum + 1;
                                    that.setData({
                                        otherimgs:that.data.otherimgs,
                                        bcNum:num
                                    })
                                } else {
                                    that.setData({
                                        [field]:Api.download + name
                                    })
                                }

                            }else{
                            }
                        }else{
                            wx.showToast({
                                title: '接口异常'+res.statusCode,
                                icon:'none'
                            });
                        }
                    },
                    fail:function(res){
                        wx.showToast({title:'图片上传失败'+res.errMsg,icon:'none'});
                    }
                })
                uploadTask.onProgressUpdate((res) => {
                    that.setData({
                    percents: res.progress
                });
                if(res.progress == 100) {
                    setTimeout(function(){
                        that.setData({
                            percents: 0
                        });
                    },1000);
                }
            })
            }
        });
    },
    detailBTRemark:function() {
        Dialog.alert({
            confirmButtonText:'关闭',
            title: '补图原因',
            message: this.data.orderVO.order.remark
        }).then(() => {
            // on close
        });
    },
    addAppra:function(){
        var user = wx.getStorageSync('user');
        var openid = user.openid;
        if (!this.data.bcNum) {
            Dialog.alert({
                confirmButtonText:'确定',
                title: '提示',
                message: '请先上传要补充的图片'
            }).then(() => {
                // on close
            });
            return;
        }

        var params = {
            otherimg:this.data.otherimgs.join(','),
            id: this.data.orderVO.order.id,
            remark: this.data.orderVO.order.remark
        };
        Toast.loading({
            mask: true,
            message: '鉴定提交中...',
            duration:0
        });
        userService.updateBC(params).then((data=>{
            if(data.result) {
            this.setData({
                active:1
            });
            Toast.loading({
                mask: true,
                message: 'AI智能初审中',
                duration:0
            });
            var id =data.obj.id;
            setTimeout(function(){
                Toast.clear();
                wx.reLaunch({
                    url: '../appraDetail/appraDetail?id=' + id
                })
            },3000);
        }else{
            wx.showToast({title:data.errorMsg,icon:'none'});
        }
    }),(error=>{
            wx.showToast({title:'addAppra接口异常',icon:'none'});
    }));
    },
});
